﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace _2D_GUI
{
    /// <summary>
    /// A portion of the screen in which to draw a C_Shape.
    /// All C_Shapes must be drawn in a C_Frame.
    /// </summary>
    class C_Frame
    {
        // Frame attributes
        C_Shape[] shapes;
        Size screenSize;
        Point position;

        /// <summary>
        /// Brief constructor.
        /// Initializes attributes.
        /// </summary>
        /// <param name="ShapeArray"></param>
        /// <param name="sizeW"></param>
        /// <param name="sizeH"></param>
        /// <param name="pos"></param>
        public C_Frame(C_Shape[] shapeArray, int sizeW, int sizeH, Point pos)
        {
            this.shapes = shapeArray;
            this.screenSize = new Size(sizeW, sizeH);
            position = pos;
        }

        /// <summary>
        /// Draws all shapes in the C_Frame.
        /// Only one shape will be drawn in a C_Frame.
        /// If more than one shape exists, recursively call draw
        /// to split the frame up into four frame quadrants.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Draw(object sender, PaintEventArgs e)
        {
            // Divide into quadrants
            try
            {
                if (shapes.Length < 1) //trouble starts here - Null handles
                {
                    // Do nothing
                }
                else if (shapes.Length == 1)
                {
                    // Draw shape if and only if there is one shape
                    if (shapes[0].Equals(shapes[0]))
                    {
                        shapes[0].SetSize(new Size((int)(screenSize.Width / (1.2)), (int)(screenSize.Height / (1.2))));

                        // Center shape in frame
                        int shapeOffsetX, shapeOffsetY;
                        shapeOffsetX = (screenSize.Width - shapes[0].GetSize().Width) / 2;
                        shapeOffsetY = (screenSize.Height - shapes[0].GetSize().Height) / 2;
                        shapes[0].SetLocation(new Point(position.X + shapeOffsetX, position.Y + shapeOffsetY));

                        shapes[0].Draw(sender, e);
                    }
                }
                else
                {
                    // Split the frame into 4 quadrants
                    C_Frame[] quadrants = new C_Frame[4];

                    // Divide shapes into 4 arrays
                    // Determine the size of each array
                    int sizeQuadrant1 = shapes.Length / 4;
                    int sizeQuadrant2 = shapes.Length / 4;
                    int sizeQuadrant3 = shapes.Length / 4;
                    int sizeQuadrant4 = shapes.Length / 4;
                    if (shapes.Length % 4 > 0)
                        sizeQuadrant1++;
                    if (shapes.Length % 4 > 1)
                        sizeQuadrant4++;
                    if (shapes.Length % 4 > 2)
                        sizeQuadrant2++;

                    // Create the new arrays
                    C_Shape[] shapesQuadrant1 = new C_Shape[sizeQuadrant1];
                    C_Shape[] shapesQuadrant2 = new C_Shape[sizeQuadrant2];
                    C_Shape[] shapesQuadrant3 = new C_Shape[sizeQuadrant3];
                    C_Shape[] shapesQuadrant4 = new C_Shape[sizeQuadrant4];

                    // Divi out the shapes
                    int i = 0;
                    for (int j = 0; j < shapes.Length; j += 4)
                    {
                        shapesQuadrant1[i] = shapes[j];
                        if (shapes.Length > j + 1)
                            shapesQuadrant4[i] = shapes[j + 1];
                        if (shapes.Length > j + 2)
                            shapesQuadrant2[i] = shapes[j + 2];
                        if (shapes.Length > j + 3)
                            shapesQuadrant3[i] = shapes[j + 3];
                        i++;
                    }

                    // Determine where the quadrants will be positioned
                    Point positionQuadrant2 = new Point(position.X + screenSize.Width / 2, position.Y);
                    Point positionQuadrant3 = new Point(position.X, position.Y + screenSize.Height / 2);
                    Point positionQuadrant4 = new Point(position.X + screenSize.Width / 2, position.Y + screenSize.Height / 2);

                    // Create the new quadrants
                    quadrants[0] = new C_Frame(shapesQuadrant1, screenSize.Width / 2, screenSize.Height / 2, position);
                    quadrants[1] = new C_Frame(shapesQuadrant2, screenSize.Width / 2, screenSize.Height / 2, positionQuadrant2);
                    quadrants[2] = new C_Frame(shapesQuadrant3, screenSize.Width / 2, screenSize.Height / 2, positionQuadrant3);
                    quadrants[3] = new C_Frame(shapesQuadrant4, screenSize.Width / 2, screenSize.Height / 2, positionQuadrant4);

                    // Draw shapes (recursive)
                    quadrants[0].Draw(sender, e);
                    quadrants[1].Draw(sender, e);
                    quadrants[2].Draw(sender, e);
                    quadrants[3].Draw(sender, e);
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// Changes the position of the frame.
        /// </summary>
        /// <param name="pos"></param>
        public void SetPosition(Point pos)
        {
            this.position = pos;
        }

        /// <summary>
        /// Changes the shapes that are within this frame.
        /// </summary>
        /// <param name="ShapeArray"></param>
        public void SetShapes(C_Shape[] shapeArray)
        {
            this.shapes = shapeArray;
        }

        /// <summary>
        /// Changes the size of the frame using two dimensions.
        /// </summary>
        /// <param name="sizeW"></param>
        /// <param name="sizeH"></param>
        public void SetSize(int sizeW, int sizeH)
        {
            this.screenSize = new Size(sizeW, sizeH);
        }

        /// <summary>
        /// Changes the size of the frame using a new size.
        /// </summary>
        /// <param name="newerSize"></param>
        public void SetSize(Size newerSize)
        {
            this.screenSize = newerSize;
        }
    }
}

